<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户注册 - 图书管理系统</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background: #f5f5f5;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .register-container {
            width: 400px;
            padding: 30px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 0 15px rgba(0,0,0,0.1);
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #333;
        }
        input[type="text"], input[type="password"], input[type="email"], select {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            font-size: 14px;
        }
        select {
            height: 40px;
        }
        button {
            width: 100%;
            padding: 12px;
            background: #28a745;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            margin-bottom: 15px;
        }
        button:hover {
            background: #218838;
        }
        .btn-secondary {
            background: #6c757d;
        }
        .btn-secondary:hover {
            background: #545b62;
        }
        .error {
            color: red;
            margin-bottom: 15px;
            text-align: center;
        }
        .success {
            color: green;
            margin-bottom: 15px;
            text-align: center;
        }
        h2 {
            text-align: center;
            margin-bottom: 30px;
            color: #333;
        }
        .form-row {
            display: flex;
            gap: 15px;
        }
        .form-row .form-group {
            flex: 1;
        }
    </style>
</head>
<body>
<div class="register-container">
    <h2>用户注册</h2>

    <% if (request.getAttribute("error") != null) { %>
    <div class="error"><%= request.getAttribute("error") %></div>
    <% } %>

    <% if (request.getAttribute("success") != null) { %>
    <div class="success"><%= request.getAttribute("success") %></div>
    <% } %>

    <form action="register" method="post">
        <div class="form-group">
            <label>用户ID *</label>
            <input type="text" name="userId" required placeholder="请输入用户ID（唯一标识）">
        </div>

        <div class="form-group">
            <label>用户名 *</label>
            <input type="text" name="username" required placeholder="请输入您的姓名">
        </div>

        <div class="form-group">
            <label>密码 *</label>
            <input type="password" name="password" required placeholder="请输入密码（至少6位）">
        </div>

        <div class="form-group">
            <label>确认密码 *</label>
            <input type="password" name="confirmPassword" required placeholder="请再次输入密码">
        </div>

        <div class="form-row">
            <div class="form-group">
                <label>邮箱</label>
                <input type="email" name="email" placeholder="请输入邮箱地址">
            </div>

            <div class="form-group">
                <label>手机号</label>
                <input type="text" name="phone" placeholder="请输入手机号">
            </div>
        </div>

        <div class="form-group">
            <label>用户角色 *</label>
            <select name="role" required>
                <option value="">请选择角色</option>
                <option value="reader">普通用户</option>
                <option value="admin">管理员</option>
            </select>
        </div>

        <button type="submit">注册</button>
        <a href="login.jsp">
            <button type="button" class="btn-secondary">返回登录</button>
        </a>
    </form>
</div>

<script>
    // 客户端表单验证
    document.querySelector('form').addEventListener('submit', function(e) {
        const password = document.querySelector('input[name="password"]').value;
        const confirmPassword = document.querySelector('input[name="confirmPassword"]').value;
        const userId = document.querySelector('input[name="userId"]').value;

        // 验证密码长度
        if (password.length < 6) {
            alert('密码长度至少6位！');
            e.preventDefault();
            return;
        }

        // 验证密码一致性
        if (password !== confirmPassword) {
            alert('两次输入的密码不一致！');
            e.preventDefault();
            return;
        }

        // 验证用户ID格式（可选：可根据需求调整）
        if (!/^[a-zA-Z0-9_]+$/.test(userId)) {
            alert('用户ID只能包含字母、数字和下划线！');
            e.preventDefault();
            return;
        }
    });
</script>
</body>
</html>